#ifndef _SLIDING_TEXT_H
#define _SLIDING_TEXT_H

#include "GraphicElement.h"

#include <string.h>
#include <iostream>
#include <mmsystem.h>
#include <tchar.h>
using namespace std;

class SlidingText: public GraphicElement
{
private:
	wstring songTitle;
	int titleLength;
	int direction;
	WCHAR title[260];
	int textStart;

	Bitmap *movingText;
	PrivateFontCollection *pfc;
	Font *font;

public:
	SlidingText(int x, int y, int width, int height, HWND hWnd): GraphicElement(x, y, width, height, hWnd)
	{
		titleLength = 0;

		direction = 1; // 1 / -1
		textStart = 0;

		movingText = new Bitmap(2000,15);



		pfc = new PrivateFontCollection();
		pfc->AddFontFile(L"pf_ronda_seven.ttf");


		// How many font families are in the private collection?
	   int countf = pfc->GetFamilyCount();

	   // Allocate a buffer to hold the array of FontFamily objects returned by
	   // the GetFamilies method.
	   FontFamily* pFontFamily = (FontFamily*)malloc(countf * sizeof(FontFamily));

	   // Get the array of FontFamily objects.
	   int found = 0;
	   pfc->GetFamilies(countf, pFontFamily, &found);

	   font = NULL;
	   if(found != 0)
	   {

			// Get the font family name.
			WCHAR                 familyName[50];
			pFontFamily[0].GetFamilyName(familyName);
 
			// Pass the family name and the address of the private collection to a
			// Font constructor.
			font = new Font(familyName, 8, FontStyleRegular,
								 UnitPixel, pfc);
	   }


	}


	~SlidingText()
	{
		delete movingText;
		delete font;
		delete pfc;
	}


	void OnDblClick()
	{
		if(checkBoundingBox())
		{

			std::wstring wide( L"Wide" ); 
			string str( songTitle.begin(), songTitle.end());

			string query = "http://www.youtube.com/results?search_query=";
			query += str;

			ShellExecuteA(NULL, "open", query.c_str(), NULL, NULL, SW_SHOWNORMAL);

		}

	}

	void Draw(HDC hdc)
	{
		if(redraw)
		{
			Bitmap bmp(dimensions->width, dimensions->height);
			Graphics *fromImg = Graphics::FromImage(&bmp);


			FontFamily  fontFamily(L"Arial");
			Font        font(&fontFamily, 12, FontStyleRegular, UnitPixel);

			SolidBrush  brushTextColor(SLIDING_TEXT_FONT_COLOR);
			SolidBrush  brushBackground(SLIDING_TEXT_BACKGROUND_COLOR);
			Pen			penBorder(SLIDING_TEXT_BORDER_COLOR);

	

			// nupiesiamas fonas po tekstu
			fromImg->FillRectangle(&brushBackground, 1, 1, dimensions->width-2, dimensions->height-2);

			if(titleLength > 0)
			{

				//WCHAR *txt = new WCHAR[260];
				//_stprintf(txt, _T("%s"), title);
				//swprintf(txt, L"%s", title);

				// atvaizduojamas tekstas
				StringFormat* stringFormat = StringFormat::GenericDefault()->Clone();
				//StringFormatFlags(left());
				//fromImg->DrawString(songTitle.c_str(), -1, &font, RectF(textStart, 0, dimensions->width-2-textStart+10, dimensions->height), stringFormat ,&brushTextColor);
				delete stringFormat;
			}

			//fromImg->DrawImage(movingText, 

			fromImg->DrawImage(movingText, textStart, 0);

			// uzdedamas remelis. Dedamas pabaigoje todel kad tekstas kartais ji dengia
			fromImg->DrawRectangle(&penBorder, 0, 0, dimensions->width-1, dimensions->height-1);


	
			Graphics    graphics(hdc);
			graphics.DrawImage(&bmp, dimensions->x, dimensions->y, dimensions->width, dimensions->height);

			delete fromImg;
			redraw = false;
		}
	}


	void Update()
	{
		if(titleLength > dimensions->width)
		{
			textStart += direction;
			if(direction == 1)
			{// stumiasi i desine
				if(textStart > 1) direction *= -1; 
			}
			else
			{// stumiasi i kaire
				if(textStart + titleLength < dimensions->width) direction *= -1; 
			}
		}
		else
		{
			direction = 1;
			textStart = 0;
		}

		UpdateGraphics();

		
	}

	void setName(wstring c)
	{

		int charLen = c.length();
		songTitle = c;
		/*for(int i = 0; i < 260; i++)
		{
			title[i] = c[i];
			if(c[i] == '\0') break;
			charLen++;
		}*/

		//title[charLen] = '\0';
		//title[0] = 'a';
		//title[1] = L'\0';

		/*Bitmap bmp(15,400);*/
		Graphics *fromImg = Graphics::FromImage(movingText);
		//fromImg->SetTextRenderingHint( TextRenderingHintAntiAlias);

		StringFormat* stringFormat = StringFormat::GenericDefault()->Clone();
		//stringFormat->SetTrimming(NULL);
		//stringFormat->SetFormatFlags(StringFormatFlagsNoFontFallback);


		

		SolidBrush  brushTextColor(SLIDING_TEXT_FONT_COLOR);


		//Font font(&FontFamily(L"PF Ronda Seven"), 8, FontStyleRegular, UnitPixel);
		RectF Rect(0,0,1000,15);
		RectF Bounds1;
		//fromImg->MeasureString(title, charLen, &font, Rect, &Bounds1);
		fromImg->MeasureString(songTitle.c_str(), charLen, font, Rect, stringFormat, &Bounds1);

		SolidBrush  brushBackground(SLIDING_TEXT_BACKGROUND_COLOR);
		
		fromImg->FillRectangle(&brushBackground, 0, 0, 1000, 15);


		if(titleLength > 0)
			{

				//WCHAR *txt = new WCHAR[260];
				//_stprintf(txt, _T("%s"), title);
				//swprintf(txt, L"%s", title);

				// atvaizduojamas tekstas
				StringFormat* stringFormat = StringFormat::GenericDefault()->Clone();
				//StringFormatFlags(left());
				fromImg->DrawString(songTitle.c_str(), -1, font, RectF(0, 0, 2000, 15), stringFormat ,&brushTextColor);
				delete stringFormat;
			}


		//Pen			penBorder(SLIDING_TEXT_BORDER_COLOR);
		//SolidBrush  brushBackground(SLIDING_TEXT_BACKGROUND_COLOR);
		//fromImg->DrawRectangle(&penBorder, 0, 0, 20, 10);
		//fromImg->FillRectangle(&brushBackground, 1, 1, dimensions->width-2, dimensions->height-2);


		titleLength = Bounds1.GetRight();
		delete fromImg;
		delete stringFormat;
		//delete[] c;


	}


};

#endif